//
//  LDAPPConfigureManager.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/7/1.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

@objc
class LDAPPConfigureManager: NSObject {
    
    //融云是否连接
    var isRongYunConnect:((status:RCConnectionStatus) ->Void)?//Bool = false
    var appConfDict:[AnyObject]?
    
    var appConfigureModel:DDAPPConfigureModel?
    
    dynamic private var xmlVersion:Float = 0
    
    func setup() {
        
        guard let path = NSBundle.mainBundle().pathForResource("LDAPPConfig.xml", ofType: nil) else {return}
        
        var data = LDCacheManager.shareInstance.userCacheManger?.objectForKey(LDAppXMLCacheKey) as? NSData
        if data == nil {
            
            data = NSData.init(contentsOfFile: path)
        }
        
        
        let cacheDict = NSDictionary.init(XMLData: data)
        appConfigureModel = DDAPPConfigureModel.yy_modelWithDictionary(cacheDict as [NSObject : AnyObject])
        
        //请求配置文件
        LDCheckPublicInfo.shareInstance.queryAppXMLConfigure({ [weak self] (data) in
            
            
            let newDict = NSDictionary.init(XMLData: data)
            let newModel = DDAPPConfigureModel.yy_modelWithDictionary(newDict as [NSObject : AnyObject])
            
            let intNewVer = Float.init(newModel!.version ?? "0") //
            let intPreVer = Float.init(self?.appConfigureModel?.version ?? "0")
            
            
            if  intNewVer > intPreVer {
                
                self?.appConfigureModel = newModel!
                NSNotificationCenter.defaultCenter().postNotificationName(LDAppXMLUpdateKey, object: nil)
                //                debugPrint("newmodel--",newModel?.version)
            }
            
            }, failure: nil)
        
        //本地xml处理
        RACObserve(self, "xmlVersion").subscribeNext { (obj) in
            
            guard let path = NSBundle.mainBundle().pathForResource("LDAPPConfig.xml", ofType: nil) else {return}
            
            data = NSData.init(contentsOfFile: path)
            let localDict = NSDictionary.init(XMLData: data)
            
            let localModel = DDAPPConfigureModel.yy_modelWithDictionary(localDict as [NSObject : AnyObject])
            let localVer = Float.init(localModel!.version ?? "0")
            
            if localVer > self.xmlVersion {
                
                self.appConfigureModel = localModel!
            }
        }
        
        //请求D+信息
        LDCheckPublicInfo.shareInstance.queryAppSystemConfigure({ [weak self] (value) in
            
            self?.appConfDict = value as [AnyObject]
            }, fail: nil)
        
    }
    
    class var configureManger: LDAPPConfigureManager {
        
        struct Instance {
            
            static let instace = LDAPPConfigureManager()
        }
        
        return Instance.instace
    }
    
    
    //
    private override init() {
        super.init()
        setup()
    }
    
}



extension LDAPPConfigureManager {
    
    //根据dict 跳转
    class func push(pushInfo:Dictionary<String,AnyObject>){
        
        
        guard let classStr = pushInfo["class"] as? String else { return }
        
        let className = NSClassFromString(classStr)
        
        guard let vc = className as? UIViewController.Type else { return }
        guard let current = getCurrentVC() as? UIViewController else { return }
        
        current.presentViewController(vc.init(), animated: true, completion: nil)
        
    }
    
    
    class func getCurrentVC() -> UIResponder {
        
        let tab = UIApplication.sharedApplication().keyWindow?.rootViewController as! UITabBarController
        
        guard let vc = tab.selectedViewController else { return UIViewController() }
        return vc
    }
    
    // 开启监听
    //    do {  reachability = try Reachability.reachabilityForInternetConnection() } catch { return }
    //    do { try reachability?.startNotifier() } catch { return }
    
}
